package com.lmx.messagetemplate.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lmx.messagetemplate.R;
import com.lmx.messagetemplate.bean.Message;
import com.lmx.messagetemplate.bean.MessageTemplate;

import java.util.List;
import java.util.Locale;

public class MessageAdapterRv extends BaseAdapterRv<Message> {

    public MessageAdapterRv(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_message, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = getItem(position);
        if (onItemClickListener != null) {
            ((ViewHolder) holder).rootView.setOnClickListener(v -> onItemClickListener.onItemClick(v, message));
        }
        ((ViewHolder) holder).refreshView(message, position);
    }

    public OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {

        void onItemClick(View view, Message data);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView tv_id;
        public TextView tv_message;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.tv_id = (TextView) rootView.findViewById(R.id.tv_id);
            this.tv_message = (TextView) rootView.findViewById(R.id.tv_message);
        }

        private void refreshView(Message bean, int position) {
            tv_id.setText(String.format(Locale.CHINA, "编号：%d", position));
            tv_message.setText(bean.getResult());
        }

    }
}
